package com.app.utils.base.j2se;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: tbyy-app->TxtFileUtil
 * @Auther: 王伟
 * @Date: 2019/12/18 14:53
 * @Description:
 */
public class CollectionFileUtil {

    /**
     * 获取收藏
     * @param uid       用户id
     * @param c_type    1 商家 3 产品
     * @return
     */
    public static List<Map<String,Object>> getCollection(long uid,int c_type){
        List<Map<String,Object>> collectionList = new ArrayList<>();
        String obj_path = getPath(c_type,uid,2);
        String str = getTexStr(obj_path);
        if(Common.isNotEmpty(str)){
            collectionList = JsonUtil.beanToList(JSONArray.parseArray(str));
        }
        return collectionList;
    }

    /**
     * 添加收藏
     * @param uid       用户id
     * @param c_type    1 商家 3 产品
     * @return
     */
    public static List<Map<String,Object>> add(long uid,int obj_id,int c_type){
        List<Map<String,Object>> collectionList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        String obj_path = getPath(c_type,uid,2);
        String str = getTexStr(obj_path);
        if(Common.isEmpty(str)){
            map.put("obj_id",obj_id);
            collectionList.add(map);
        }else{
            collectionList = JsonUtil.beanToList(JSONArray.parseArray(str));
            Boolean b = false;
            for (Map<String,Object> obj:collectionList) {
                int obj_id_c = Convert.objToInt(obj.get("obj_id"));
                if(obj_id==obj_id_c){
                    b=true;
                }
            }
            if(!b){
                map.put("obj_id",obj_id);
                collectionList.add(map);
            }
        }
        obj_path = getPath(c_type,uid,1);
        addCollection(collectionList,obj_path);
        return collectionList;
    }

    /**
     * 删除收藏
     * @param uid       用户id
     * @param obj_id
     * @param c_type    1 商家 3 产品
     * @return
     */
    public static List<Map<String,Object>> del(long uid,int obj_id,int c_type){
        List<Map<String,Object>> collectionList = new ArrayList<>();
        List<Map<String,Object>> list = new ArrayList<>();
        String obj_path = getPath(c_type,uid,2);
        String str = getTexStr(obj_path);
        if(Common.isNotEmpty(str)){
            collectionList = JsonUtil.beanToList(JSONArray.parseArray(str));
            for (Map<String,Object> obj:collectionList) {
               int obj_id_c = Convert.objToInt(obj.get("obj_id"));
               if(obj_id!=obj_id_c){
                   list.add(obj);
               }
            }
        }
        obj_path = getPath(c_type,uid,1);
        addCollection(list,obj_path);
        return collectionList;
    }

    /**
     * 添加文件到oss中
     * @param list
     * @param path
     */
    public static void addCollection(List<Map<String,Object>> list,String path){
        String s = JSONObject.toJSONString(list);
        //转化为流
        InputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(s.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        OssFileUtil.putStream(path,inputStream);
    }

    public static String getTexStr(String filePath){
        int HttpResult; // 服务器返回的状态
        String ee = "";
        try
        {
            URL url =new URL(filePath); // 创建URL
            URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
            urlconn.connect();
            HttpURLConnection httpconn =(HttpURLConnection)urlconn;
            HttpResult = httpconn.getResponseCode();
            if(HttpResult != HttpURLConnection.HTTP_OK) {
                System.out.println("无法连接到");
            } else {
                InputStreamReader isReader = new InputStreamReader(urlconn.getInputStream(),"UTF-8");
                BufferedReader reader = new BufferedReader(isReader);
                StringBuilder buffer = new StringBuilder();
                String line; // 用来保存每行读取的内容
                line = reader.readLine(); // 读取第一行
                while (line != null) { // 如果 line 为空说明读完了
                    buffer.append(line); // 将读到的内容添加到 buffer 中
                    buffer.append(" "); // 添加换行符
                    line = reader.readLine(); // 读取下一行
                }
                System.out.print(buffer.toString());
                ee = buffer.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ee;
    }

    /**
     *
     * @param c_type
     * @param uid
     * @param r_s_type 读写类型 1 写 2 读
     * @return
     */
    public static String getPath(int c_type ,long uid,int r_s_type){
        String obj_path ="https://aloss.taomeimei.net/back/user/collection";
        if(r_s_type==1){
            obj_path="back/user/collection";
        }
        if(c_type==1){
            obj_path = obj_path+"/merchant/"+uid+".txt";
        }
        if(c_type==2){
            obj_path = obj_path+"/doctor/"+uid+".txt";
        }
        if(c_type==3){
            obj_path = obj_path+"/goods/"+uid+".txt";
        }
        return obj_path;
    }
}
